require 'family_tree_api/person/assertions/person'
require 'family_tree_api/person/assertions/base'
module FamilyTreeApi
  module Person
    module Assertion
      
      class Relationship < Base
        attr_accessor :scope, :spouse, :parent, :child
        
        def self.new_spouse(spouse_ref, gender, options = {})
          r = self.new
          r.scope = 'couple'
          r.tempId = options[:tempId] if options[:tempId]
          r.spouse = Assertion::Spouse.new
          r.spouse.ref = spouse_ref
          r.spouse.role = case gender
            when 'Male'
              'Woman'
            when 'Female'
              'Man'
            else
              'Unknown'
          end
          r
        end
        
        def self.parse(relationship_element)
          rel = self.new
          rel.parse_attributes(relationship_element)
          rel.scope = relationship_element.attributes["scope"]
          relationship_element.each do |e|
            case e.name
            when 'spouse'
              rel.spouse = Assertion::Spouse.parse(e)
            when 'parent'
              rel.parent = Assertion::Parent.parse(e)
            when 'child'
              rel.child = Assertion::Child.parse(e)
            end
          end
          rel
        end
      
        def to_xml
          e = super
          e.add_attribute('scope', @scope)
          e.add_element(@spouse.to_xml) if @spouse
          e.add_element(@parent.to_xml) if @parent
          e.add_element(@child.to_xml) if @child
          e
        end
      
      end
      
    end
  end
end